package com.chengyu.core.service.goods.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.chengyu.core.dao.GoodsBrandCategoryDao;
import com.chengyu.core.model.GoodsBrandCategory;
import com.chengyu.core.service.goods.GoodsBrandCategoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
*  GoodsBrandCategoryServiceImpl 接口实现
*/
@Service
public class GoodsBrandCategoryServiceImpl extends ServiceImpl<GoodsBrandCategoryDao, GoodsBrandCategory> implements GoodsBrandCategoryService {

    @Resource
    private GoodsBrandCategoryDao dao;

    /**
     * 通过品牌id删除
     * @param bid 品牌id
     * @return Boolean
     */
    @Override
    public Boolean deleteByBid(Integer bid) {
        LambdaUpdateWrapper<GoodsBrandCategory> wrapper = Wrappers.lambdaUpdate();
        wrapper.eq(GoodsBrandCategory::getBid, bid);
        return dao.delete(wrapper) > 0;
    }

    /**
     * 获取关联的分类id
     * @param brandId 品牌id
     * @return List
     */
    @Override
    public List<GoodsBrandCategory> getListByBrandId(Integer brandId) {
        LambdaQueryWrapper<GoodsBrandCategory> lqw = Wrappers.lambdaQuery();
        lqw.eq(GoodsBrandCategory::getBid, brandId);
        return dao.selectList(lqw);
    }

    /**
     * 是否存在分类
     * @param categoryId 分类id
     * @return Boolean
     */
    @Override
    public Boolean isExistCategory(Integer categoryId) {
        LambdaQueryWrapper<GoodsBrandCategory> lqw = Wrappers.lambdaQuery();
        lqw.eq(GoodsBrandCategory::getCid, categoryId);
        lqw.last(" limit 1");
        GoodsBrandCategory goodsBrandCategory = dao.selectOne(lqw);
        return ObjectUtil.isNotNull(goodsBrandCategory);
    }
}

